package com.ymatou.iapi.productmanager.testcase;

import com.ymatou.iapi.productmanager.parameter.GetPidFromProductIdBean;
import com.ymatou.iapi.productmanager.parameter.product.AddSellerProductRequest;
import com.ymatou.iapi.productmanager.service.GetPidFromProductIdCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.ProductManagerCallServiceV2;
import com.ymttest.business.service.ProductManagerCallServiceV3;
import com.ymttest.database.model.YmtProductsI;
import com.ymttest.database.sqlwapper.YmtProductsIWapper;
import com.ymttest.utils.EnvSetup;
import org.junit.*;
import org.junit.experimental.categories.Category;

import java.util.Arrays;

/**
*根据商品编号换商品数字编号PID
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_GetPidFromProductId{
	private static GetPidFromProductIdBean getpidfromproductidBean;
	private static GetPidFromProductIdCall getpidfromproductidCall;
	private static int sellerId = Integer.parseInt(EnvSetup.getData("productpriceSellerId"));
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("根据商品编号换商品数字编号PID");
	}

	@Before
	public void caseUp() {
		getpidfromproductidBean=new GetPidFromProductIdBean();
		getpidfromproductidCall=new GetPidFromProductIdCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getpidfromproductidCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetPidFromProductId_01() {
		Logger.start(true,"根据商品编号换商品数字编号PID");
		try {
			AddSellerProductRequest addpro = ProductManagerCallServiceV3.initAddSellerProductRequest(sellerId);
			String productId = ProductManagerCallServiceV3.AddSellerProduct(addpro);
			getpidfromproductidBean.setProductId(productId);
			getpidfromproductidBean.setSellerId(sellerId);
			getpidfromproductidCall.setData(getpidfromproductidBean);
			getpidfromproductidCall.callService();
			Logger.verifyEquals("200",getpidfromproductidCall.getString("Code"),"验证code");

			YmtProductsIWapper ymtProductsIWapper = new YmtProductsIWapper();
			YmtProductsI ymtProductsIS = ymtProductsIWapper.selectProductByProductId(productId).get(0);
			Logger.verifyEquals(getpidfromproductidCall.getJsonObject().get("Data").getAsJsonObject().get("pid").getAsInt(),ymtProductsIS.getIpid(),"验证pid");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

    @Test
    @Category({P1.class})
    @TestCase
    public void Tc_GetPidFromProductId_02() {
        Logger.start(true,"删除商品编号换商品数字编号PID,获取失败");
        try {
            AddSellerProductRequest addpro = ProductManagerCallServiceV3.initAddSellerProductRequest(sellerId);
            String productId = ProductManagerCallServiceV3.AddSellerProduct(addpro);
            ProductManagerCallServiceV2.batchDeleteProducts(Arrays.asList(productId),sellerId);

            getpidfromproductidBean.setProductId(productId);
            getpidfromproductidBean.setSellerId(sellerId);
            getpidfromproductidCall.setData(getpidfromproductidBean);
            getpidfromproductidCall.callService();
            Logger.verifyEquals("200",getpidfromproductidCall.getString("Code"),"验证code");
            Logger.verifyEquals(getpidfromproductidCall.getJsonObject().get("Data").getAsJsonObject().get("pid").isJsonNull(),true,"验证pid");

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category({P1.class})
    @TestCase
    public void Tc_GetPidFromProductId_03() {
        Logger.start(true,"sellerId为空,获取失败");
        try {
            AddSellerProductRequest addpro = ProductManagerCallServiceV3.initAddSellerProductRequest(sellerId);
            String productId = ProductManagerCallServiceV3.AddSellerProduct(addpro);
            ProductManagerCallServiceV2.batchDeleteProducts(Arrays.asList(productId),sellerId);

            getpidfromproductidBean.setProductId(productId);
            getpidfromproductidCall.setData(getpidfromproductidBean);
            getpidfromproductidCall.callService();
            Logger.verifyEquals("200",getpidfromproductidCall.getString("Code"),"验证code");
            Logger.verifyEquals(getpidfromproductidCall.getJsonObject().get("Data").getAsJsonObject().get("pid").isJsonNull(),true,"验证pid");

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category({P1.class})
    @TestCase
    public void Tc_GetPidFromProductId_04() {
        Logger.start(true,"productId为空,获取失败");
        try {
            AddSellerProductRequest addpro = ProductManagerCallServiceV3.initAddSellerProductRequest(sellerId);
            String productId = ProductManagerCallServiceV3.AddSellerProduct(addpro);
            ProductManagerCallServiceV2.batchDeleteProducts(Arrays.asList(productId),sellerId);

            getpidfromproductidBean.setSellerId(sellerId);
            getpidfromproductidCall.setData(getpidfromproductidBean);
            getpidfromproductidCall.callService();
            Logger.verifyEquals("400",getpidfromproductidCall.getString("Code"),"验证code");

        } catch (Exception e) {
            Logger.fail(e);
        }
    }
}